#pragma once

#include <iostream>
#include <pthread.h>
#include "Mutex.hpp"

//线程条件变量的封装

namespace CondModule
{
    using namespace MutexModule;
    class Cond
    {
    public:
        Cond()
        {
            int n = ::pthread_cond_init(&_cond, nullptr);
            if(n != 0)
            {
                std::cerr << "cond init error" << std::endl;
            }
        }
        void Wait(Mutex &mutex)
        {
            int n = ::pthread_cond_wait(&_cond, mutex.MutexAddr());
            if(n != 0)
            {
                std::cerr << "cond wait error" << std::endl;
            }
        }
        void Notify()
        {
            int n = ::pthread_cond_signal(&_cond);
            if(n != 0)
            {
                std::cerr << "cond signal error" << std::endl;
            }
        }
        void NotifyAll()
        {
            int n = ::pthread_cond_broadcast(&_cond);
            if(n != 0)
            {
                std::cerr << "cond broadcast error" << std::endl;
            }
        }
        ~Cond()
        {
            int n = ::pthread_cond_destroy(&_cond);
            if(n != 0)
            {
                std::cerr << "cond destroy error" << std::endl;
            }
        }
    private:
        pthread_cond_t _cond;
    };
}